package by.grsu.kononovich.page.simple;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.AutoDao;
import by.grsu.kononovich.entity.Auto;

public class AutosListPage extends WebPage{
	public AutosListPage() {
		super();

		
		final AutoDao autoDao = new AutoDao(MyFirstWebApplication.DATA_XML_FOLDER);

		
		final List<Auto> allAuto = autoDao.getAll();

		
		add(new DataView<Auto>("table-rows", new ListDataProvider<Auto>(allAuto)) {
			@Override
			protected void populateItem(final Item<Auto> item) {
				final Auto auto = item.getModelObject();
				
				item.add(new Label("id", auto.getId()));
				item.add(new Label("mark", auto.getMark()));
				item.add(new Label("model", auto.getModel()));
				item.add(new Label("freedom", auto.getFreedom()));
				item.add(new Label("cost", auto.getCost()));

				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						
						setResponsePage(new AutoEditPage(auto));
						System.out.println("Auto"+auto.getMark()+" "+auto.getModel()+" "+auto.getFreedom()+" "+auto.getCost()+" edit");
					}
				});

				
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
					
						autoDao.delete(auto.getId());
						
						setResponsePage(new AutosListPage());
						System.out.println("Auto"+auto.getMark()+" "+auto.getModel()+" "+auto.getFreedom()+" "+auto.getCost()+" deleted");
					}
				});
			}
		});

		
		add(new Link("createLink") {
			@Override
			public void onClick() {
				
				setResponsePage(new AutoEditPage(new Auto()));
				System.out.println("button CREAT NEW AUTO pressed");
			}
		});
		}

}
